package com.baizhi.service.impl;

import com.baizhi.dao.ProductDao;
import com.baizhi.entity.Cart;
import com.baizhi.entity.OrderDetail;
import com.baizhi.entity.Product;
import com.baizhi.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author yystart
 * @create 2019-10-04 17:53
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private ProductDao dao;

    @Override
    public Map selectCart(HttpSession session) {

        Cart cart = (Cart) session.getAttribute("cart");

        List<OrderDetail> list = new ArrayList<>();

        if (cart != null) {
            Map<Integer, OrderDetail> orderDetailMap = cart.getOrderDetailMap();
            for (Integer key : orderDetailMap.keySet()) {
                list.add(orderDetailMap.get(key));
            }
        }

        Map map = new HashMap();
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");

        return map;
    }

    @Override
    public Map insert(HttpSession session, Integer productId) {
        Cart cart = (Cart) session.getAttribute("cart");
        //如果没有购物车就创建一个购物车
        if (cart == null) cart = new Cart();
        Product product = dao.selectOne(productId);
        Map<Integer, OrderDetail> orderDetailMap = cart.getOrderDetailMap();
        //如果购物车中已经存在这个商品那么这个商品的数量 + 1
        if(orderDetailMap.containsKey(productId)){
            OrderDetail orderDetail = orderDetailMap.get(productId);
            orderDetail.setQuantity(orderDetail.getQuantity() + 1);
            orderDetail.setUpdateTime(new Date());
            orderDetailMap.put(productId,orderDetail);
        } else {
            //否则将这个商品添加到购物车
            OrderDetail detail = new OrderDetail();
            detail.setQuantity(1);
            detail.setProduct(product);
            detail.setCreateTime(new Date());
            orderDetailMap.put(productId,detail);
        }

        //更新购物车总价格
        cart = updateAmount(cart);
        //将更新后的购物车放入session
        session.setAttribute("cart",cart);

        Map map = new HashMap();
        map.put("isInsert",true);

        return map;
    }

    @Override
    public OrderDetail selectOne(HttpSession session, Integer productId) {
        Cart cart = (Cart) session.getAttribute("cart");
        Map<Integer, OrderDetail> detailMap = cart.getOrderDetailMap();
        OrderDetail detail = detailMap.get(productId);
        return detail;
    }

    @Override
    public Map update(HttpSession session, Integer productId, Integer quantity) {
        Cart cart = (Cart) session.getAttribute("cart");
        Map<Integer, OrderDetail> detailMap = cart.getOrderDetailMap();
        OrderDetail detail = detailMap.get(productId);
        detail.setQuantity(quantity);
        detail.setUpdateTime(new Date());
        detailMap.put(productId,detail);

        //更新购物车总价格
        cart = updateAmount(cart);
        //将更新后的购物车放入session
        session.setAttribute("cart",cart);

        Map map = new HashMap();
        map.put("isUpdate",true);

        return map;
    }

    @Override
    public Map delete(HttpSession session, Integer productId) {
        Cart cart = (Cart) session.getAttribute("cart");
        Map<Integer, OrderDetail> detailMap = cart.getOrderDetailMap();
        detailMap.remove(productId);

        //更新购物车总价格
        cart = updateAmount(cart);
        //将更新后的购物车放入session
        session.setAttribute("cart",cart);

        Map map = new HashMap();
        map.put("isDelete",true);

        return map;
    }

    /**
     * 统计总金额
     */
    public Cart updateAmount(Cart cart) {
        Map<Integer, OrderDetail> map = cart.getOrderDetailMap();
        Integer amount = 0;
        for (Integer key : map.keySet()) {
            amount += map.get(key).getQuantity() * map.get(key).getProduct().getPrice();
        }
        cart.setAmount(amount);
        return cart;
    }

}
